﻿using System;
using System.Reflection;
using System.Configuration;
namespace HHDALFactory
{
	/// <summary>
    /// Abstract Factory pattern to create the DAL。
    /// 如果在这里创建对象报错，请检查web.config里是否修改了<add key="DAL" value="Maticsoft.SQLServerDAL" />。
	/// </summary>
	public sealed class DataAccess
	{
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];        
		public DataAccess()
		{ }

        #region CreateObject 

		//不使用缓存
        private static object CreateObjectNoCache(string AssemblyPath,string classNamespace)
		{		
			try
			{
				object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);	
				return objType;
			}
			catch//(System.Exception ex)
			{
				//string str=ex.Message;// 记录错误日志
				return null;
			}			
			
        }
		//使用缓存
		private static object CreateObject(string AssemblyPath,string classNamespace)
		{			
			object objType = DataCache.GetCache(classNamespace);
			if (objType == null)
			{
				try
				{
					objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);					
					DataCache.SetCache(classNamespace, objType);// 写入缓存
				}
				catch//(System.Exception ex)
				{
					//string str=ex.Message;// 记录错误日志
				}
			}
			return objType;
		}
        #endregion

        #region CreateSysManage
        //public static HHIDAL.ISysManage CreateSysManage()
        //{
        //    //方式1			
        //    //return (HHIDAL.ISysManage)Assembly.Load(AssemblyPath).CreateInstance(AssemblyPath+".SysManage");

        //    //方式2 			
        //    string classNamespace = AssemblyPath+".SysManage";	
        //    object objType=CreateObject(AssemblyPath,classNamespace);
        //    return (HHIDAL.ISysManage)objType;		
        //}
		#endregion
             
        
   
		/// <summary>
		/// 创建AboutUsClass数据层接口。
		/// </summary>
		public static HHIDAL.IAboutUsClass CreateAboutUsClass()
		{

			string ClassNamespace = AssemblyPath +".AboutUsClass";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IAboutUsClass)objType;
		}


		/// <summary>
		/// 创建AboutUsInfo数据层接口。
		/// </summary>
		public static HHIDAL.IAboutUsInfo CreateAboutUsInfo()
		{

			string ClassNamespace = AssemblyPath +".AboutUsInfo";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IAboutUsInfo)objType;
		}


		/// <summary>
		/// 创建DownCoreClass数据层接口。
		/// </summary>
		public static HHIDAL.IDownCoreClass CreateDownCoreClass()
		{

			string ClassNamespace = AssemblyPath +".DownCoreClass";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IDownCoreClass)objType;
		}


		/// <summary>
		/// 创建DownCores数据层接口。
		/// </summary>
		public static HHIDAL.IDownCores CreateDownCores()
		{

			string ClassNamespace = AssemblyPath +".DownCores";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IDownCores)objType;
		}

        /// <summary>
        /// 创建DownloadCodes数据层接口。
        /// </summary>
        public static HHIDAL.IDownloadCodes CreateDownloadCodes()
        {

            string ClassNamespace = AssemblyPath + ".DownloadCodes";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HHIDAL.IDownloadCodes)objType;
        }

		/// <summary>
		/// 创建LeaveWord数据层接口。
		/// </summary>
		public static HHIDAL.ILeaveWord CreateLeaveWord()
		{

			string ClassNamespace = AssemblyPath +".LeaveWord";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.ILeaveWord)objType;
		}


		/// <summary>
		/// 创建Links数据层接口。
		/// </summary>
		public static HHIDAL.ILinks CreateLinks()
		{

			string ClassNamespace = AssemblyPath +".Links";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.ILinks)objType;
		}


		/// <summary>
		/// 创建NewClass数据层接口。
		/// </summary>
		public static HHIDAL.INewClass CreateNewClass()
		{

			string ClassNamespace = AssemblyPath +".NewClass";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.INewClass)objType;
		}


		/// <summary>
		/// 创建News数据层接口。
		/// </summary>
		public static HHIDAL.INews CreateNews()
		{

			string ClassNamespace = AssemblyPath +".News";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.INews)objType;
		}


		/// <summary>
		/// 创建PageKeys数据层接口。
		/// </summary>
		public static HHIDAL.IPageKeys CreatePageKeys()
		{

			string ClassNamespace = AssemblyPath +".PageKeys";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IPageKeys)objType;
		}

        /// <summary>
        /// 创建PageImgs数据层接口。
        /// </summary>
        public static HHIDAL.IPageImgs CreatePageImgs()
        {

            string ClassNamespace = AssemblyPath + ".PageImgs";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HHIDAL.IPageImgs)objType;
        }

		/// <summary>
		/// 创建PagePower数据层接口。
		/// </summary>
		public static HHIDAL.IPagePower CreatePagePower()
		{

			string ClassNamespace = AssemblyPath +".PagePower";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IPagePower)objType;
		}

        /// <summary>
        /// 创建Pages数据层接口。
        /// </summary>
        public static HHIDAL.IPages CreatePages()
        {
            string ClassNamespace = AssemblyPath + ".Pages";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (HHIDAL.IPages)objType;
        }

		/// <summary>
		/// 创建PageStatistics数据层接口。
		/// </summary>
		public static HHIDAL.IPageStatistics CreatePageStatistics()
		{

			string ClassNamespace = AssemblyPath +".PageStatistics";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IPageStatistics)objType;
		}


		/// <summary>
		/// 创建Partners数据层接口。
		/// </summary>
		public static HHIDAL.IPartners CreatePartners()
		{

			string ClassNamespace = AssemblyPath +".Partners";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IPartners)objType;
		}


		/// <summary>
		/// 创建ProductClass数据层接口。
		/// </summary>
		public static HHIDAL.IProductClass CreateProductClass()
		{

			string ClassNamespace = AssemblyPath +".ProductClass";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IProductClass)objType;
		}


		/// <summary>
		/// 创建Products数据层接口。
		/// </summary>
		public static HHIDAL.IProducts CreateProducts()
		{

			string ClassNamespace = AssemblyPath +".Products";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IProducts)objType;
		}


		/// <summary>
		/// 创建Roles数据层接口。
		/// </summary>
		public static HHIDAL.IRoles CreateRoles()
		{

			string ClassNamespace = AssemblyPath +".Roles";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IRoles)objType;
		}


		/// <summary>
		/// 创建Solutions数据层接口。
		/// </summary>
		public static HHIDAL.ISolutions CreateSolutions()
		{

			string ClassNamespace = AssemblyPath +".Solutions";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.ISolutions)objType;
		}


		/// <summary>
		/// 创建SolutionTypes数据层接口。
		/// </summary>
		public static HHIDAL.ISolutionTypes CreateSolutionTypes()
		{

			string ClassNamespace = AssemblyPath +".SolutionTypes";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.ISolutionTypes)objType;
		}


		/// <summary>
		/// 创建SuccessfulCase数据层接口。
		/// </summary>
		public static HHIDAL.ISuccessfulCase CreateSuccessfulCase()
		{

			string ClassNamespace = AssemblyPath +".SuccessfulCase";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.ISuccessfulCase)objType;
		}


		/// <summary>
		/// 创建UserRegLogs数据层接口。
		/// </summary>
		public static HHIDAL.IUserRegLogs CreateUserRegLogs()
		{

			string ClassNamespace = AssemblyPath +".UserRegLogs";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IUserRegLogs)objType;
		}


		/// <summary>
		/// 创建Users数据层接口。
		/// </summary>
		public static HHIDAL.IUsers CreateUsers()
		{

			string ClassNamespace = AssemblyPath +".Users";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (HHIDAL.IUsers)objType;
		}

}
}